
#ifndef MIRCROPHONEIODEVICE_H
#define MIRCROPHONEIODEVICE_H

#include <QIODevice>
#include <QMutex>
#include <QWaitCondition>

/**
 * @brief The MircrophoneIODevice class  我这个类实现的是双向数据流的接口，因此必须重写readData 和 writeData
 * 本类实现的工作是：获取麦克风的数据并写入到本类中作为麦克风的输出源，同时本类作为音频播放器的输入源
 */

using SampleType = float;

class MircrophoneIODevice : public QIODevice
{
public:
    explicit MircrophoneIODevice(QObject *parent = nullptr);
    void start();
    void stop();
    bool bufferIsEmpty();
    void computeAudioData(char *data, qint64 bytesRead);

protected:
    qint64 readData(char *data, qint64 maxSize) override;
    qint64 writeData(const char *data, qint64 maxSize) override;
    qint64 bytesAvailable() const override;

private:
    QByteArray buffer;
    mutable QMutex mutex; //mutable表示该变量可以在const成员函数或const对象中被修改
    QWaitCondition condition;
    bool emitSign;
};

#endif // MIRCROPHONEIODEVICE_H
